﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.interfaces;
using Procure.Data.Context;

namespace Procure.Web.attribute
{
    public class attributeTasks : ITaskReturnUrl
    {
        public int UserId { get; set; }
        public int TypeID { get; set; } // Data Type Id
        public int ParentID { get; set; }
        public string Alias { get; set; }
        public string ReturnUrl { get; private set; }
        
        public bool Save()
        {
            try
            {
                ProcureDataContext context = DataFactory.GetDataContext();
                Procure.Data.Context.Attribute a = new Procure.Data.Context.Attribute();
                a.Name = Alias;
                a.DataTypeId = 1;
                context.Attributes.InsertOnSubmit(a);
                context.SubmitChanges();

                ReturnUrl = string.Format("Procure/editProductType.aspx?id={0}", a.Id);
            }
            catch (Exception ex)
            {
                ReturnUrl = string.Format("Procure/{0}.aspx", ex.Message);
            }

            ReturnUrl = string.Format("Procure/editAttribute.aspx?id={0}", "1");

            return true;
        }

        public bool Delete()
        {
            Procure.Data.Context.ProcureDataContext context = DataFactory.GetDataContext();

            Procure.Data.Context.Attribute a = context.Attributes.FirstOrDefault(pt => pt.Id == ParentID);
            if (a != null)
            {
                context.Attributes.DeleteOnSubmit(a);
                context.SubmitChanges();
                return true;
            }
            else
                return false;
        }

        public attributeTasks()
        {

        }
    }
}